<?php
/**
 * Created by PhpStorm.
 * User: zyh
 * Date: 2018/5/9
 * Time: 下午12:12
 */

namespace app\admin\controller;


use think\Controller;

class User extends Controller
{
    public function member_list()
    {
        $users = \app\admin\model\User::all();
//        print_r($users);die;
        $this->assign("users",$users);
        return view();
    }

    public function member_add()
    {
        $user = new \app\admin\model\User();
        if ($id = input('param.id')){
            $user = $user::get($id);
            $this->assign("user",$user);
        }

        if ($arr = input("post.")){

            if ($user->user_add($arr)){
                return ['status'=>1];
            }else{
                return ['status'=>0];
            }
        }
        $this->assign("password",rand(100000,999999));
        return view();
    }

    public function recharge($id)
    {
        $user = \app\admin\model\User::get($id);
        if ($score = input("post.score")){
            $rs = $user->setInc('score',$score);
            if ($rs){
                return 1;
            }else{
                return 0;
            }
        }
        $this->assign("user",$user);
        return view();
    }

    public function member_show()
    {
        $user = \app\admin\model\User::get(input("param.id"));
        $this->assign("user",$user);
        return view();
    }

    public function change_password($id)
    {
        $user = \app\admin\model\User::get($id);
        if ($arr = input("post.")){
            $rs = $user->save(['password'=>md5(input('post.newpassword')),'id'=>$id]);
            if ($rs){
                return 1;
            }else{
                return 0;
            }
        }
        $this->assign("user",$user);
        return view();
    }

    public function member_stop($id)
    {
        $user = \app\admin\model\User::get($id);
        return $user->save(['status'=>0,'id'=>$id]);
    }

    public function member_enable($id)
    {
        $user = \app\admin\model\User::get($id);
        return $user->save(['status'=>1,'id'=>$id]);
    }
}